/*
	This script file is part of the Sonic 3 A.I.R. script software distribution.
	Copyright (C) 2017-2024 by Eukaryot

	Published under the GNU GPLv3 open source software license, see license.txt
	or https://www.gnu.org/licenses/gpl-3.0.en.html
*/



//-------------------------------------------------------//
// Cutscene with Knuckles after AIZ 2 Boss               //
//-------------------------------------------------------//

// Knuckles in action again!

//# address-hook(0x061f98) end(0x061fb0)
function void fn061f98()
{
	// Targets:
	//  - 0x061fc2	-> objA0.base_state = 0x00	-> Initialization
	//  - 0x062014	-> objA0.base_state = 0x02	-> Waiting time before Knuckles starts
	//  - 0x062022	-> objA0.base_state = 0x04	-> Knuckles walks in
	//  - 0x06206e	-> objA0.base_state = 0x06  -> Knuckles chuckling
	//  - 0x0620aa	-> objA0.base_state = 0x08	-> Knuckles jumps onto the button and back, triggers bridge collapse in between
	//  - 0x0620e4	-> objA0.base_state = 0x0a  -> Back to chuckling
	call tableLookupAddress(0x061fb6, objA0.base_state)

	Object.loadSpritePatternsForVDP(0x06666a)
	DrawObject()
}


//# address-hook(0x061fc2) end(0x062012)
function void fn061fc2()
{
	if (SetupObjectAttributesSlotted(0x0663ba))
		return

	objA0.position.x.u16 = 0x4b8e
	objA0.position.y.u16 = 0x017d
	objA0.render_flags |= render_flag.FLIP_X
	fn065dd6()

	objA0.countdown_value = 119
	objA0.countdown_callback = 0x06201a

	CutsceneKnuckles.loadPalette()

	// "spawnSimpleChildObjects(0x06655a)" replaced by:
	spawnSimpleChildObjects(0x0620ea, 1)				// Invisible collision object
	if (_equal())
	{
		objA1.position.x.u16 = 0x4b08
		objA1.position.y.u16 = 0x0178
	}
}


//# address-hook(0x062022) end(0x062050)
function void fn062022()
{
	D0.u16 = objA0.position.x.u16 - 2
	if (D0.u16 >= 0x4b3c)
	{
		objA0.position.x.u16 = D0.u16
		Object.animationProgress(0x06669f)		// Cutscene Knuckles walking animation
		return
	}

	// Start Knuckles chuckling
	objA0.base_state = 0x06
	objA0.render_flags &= ~render_flag.FLIP_X
	objA0.countdown_value = 95
	objA0.countdown_callback = 0x06207a
	fn062056()
}


//# address-hook(0x06206e) end(0x062074)
function void fn06206e()
{
	Object.AnimationProgressByLocalA1()
	Object.CountdownAndTrigger()
}


//# address-hook(0x0620e4) end(0x0620e4)
function void fn0620e4()
{
	Object.AnimationProgressByLocalA1()
}


//# address-hook(0x0620ea) end(0x06210c)
function void fn0620ea()
{
	A1 = 0xffff0000 + u16[A0 + 0x46]
	if (u8[A1 + 0x2a] & 0x80)
	{
		UnloadObject()
		return
	}

	objA0.box_size.x = 0x08
	D1 = 0x13
	D2.u16 = 0x20
	D3.u16 = 0x40
	D4.u16 = objA0.position.x.u16
	CheckCharacterOnClimbableObject()
}


//# address-hook(0x06201a) end(0x062020)
function void fn06201a()
{
	// Start Knuckles walking in
	objA0.base_state = 0x04
}


//# address-hook(0x06207a) end(0x06207a)
function void fn06207a()
{
	// Start Knuckles jumping back and forth on the button
	objA0.base_state = 0x08
	fn062080()
}
